package com.ken.blog.filter;

import com.ken.blog.component.CategoryComponent;
import com.ken.blog.domain.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Component
public class WordFilter implements HandlerInterceptor {

    @Autowired
    private CategoryComponent categoryComponent;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception {
        System.out.println("===============1、在处理请求之前调用（调用Controller之前调用）==================");

        //处理标题文字，如果包含有"扑街"字样，直接请求失败
        String title = request.getParameter("title");
        if (title != null) {
            int isExist = title.lastIndexOf("扑街");
            if (isExist == 0) {
                throw new Exception("具有限制性词汇，请修改");
//                return false;
            }
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView) throws Exception {
        System.out.println("================2、在处理请求之后调用，视图渲染之前调用（执行Controller之后，view渲染之前）================");

        modelAndView.addObject("blogTitle", "Spring Boot Blog Admin");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex) throws Exception {
        System.out.println("=================3、在整个请求结束调用后执行，DispatcherServlet渲染了view视图之后调用===========");
    }
}